package linuxbox.common.taglib;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
import java.io.*;

import linuxbox.BoxStatic;

public class ListHeader extends BodyTagSupport
{
  protected String[] headers = null;
  protected String[] sortby = null;
  protected String curSortBy = null;
  protected String action = null;
  protected boolean sortAsc = true;

  public void setHeaders(String [] h)
  {
    headers = h;
  }

  public void setSortby(String [] s)
  {
    sortby = s;
  }

  public void setCurSortBy(String s)
  {
    curSortBy = s;
  }

  public void setAction(String s)
  {
    action = s;
  }

  public void setSortAsc(boolean b)
  {
    sortAsc = b;
  }

  public int doStartTag() throws JspException
  {
    try
    {
      JspWriter out = pageContext.getOut();

      out.print("<TR bgColor=#7EC1F3>");
      for(int i = 0; i < headers.length; i++)
      {
        if (sortby[i].length()>0)
        {
        out.print("<TH ><a href=\"");
        out.print(action);
        if (action.charAt(action.length()-1) == '&')
          out.print("SORTBY=");
        else
          out.print("?SORTBY=");
        out.print(sortby[i]);
        out.print("&ASC=");
        out.print(!sortAsc);
        out.print("\">");
        out.print(headers[i]);
        out.print("&nbsp;");
        if (curSortBy.compareTo(sortby[i]) == 0)
        {
          if (sortAsc)
            out.print("<img SRC=\"" + BoxStatic.imageDir + "asc.gif\" border=0>");
          else
            out.print("<img SRC=\"" + BoxStatic.imageDir + "desc.gif\" border=0>");
        }
        out.print("</a></TH>");
      }
      else
      {
        out.print("<TH>");
        out.print(headers[i]);
        out.print("</TH>");
      }
      }
      out.print("</TR>");

      out.flush();
    }
    catch(Exception e)
    {
      e.printStackTrace();
      throw new JspException(e.getMessage());
    }

    return SKIP_BODY;
  }
}
